﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Xml;

namespace Possan.Localization
{
	public class Library
	{ 

		public string GetPhrase(string namespacename, string originalphrase)
		{
			return GetPhrase(namespacename, originalphrase, CultureInfo.CurrentUICulture);
		}

		public string GetPhrase(string namespacename, string originalphrase, CultureInfo locale)
		{
			return GetPhrase(namespacename, originalphrase, locale.Name);
		}

		public string GetPhrase(string namespacename, string originalphrase, string locale)
		{
			namespacename = namespacename.ToLower();

			string ret = originalphrase;

			if (namespaces.ContainsKey(namespacename))
			{
				LibraryNamespace libns = namespaces[namespacename];
				ret = libns.GetPhrase(originalphrase, locale);
			}

			return ret;
		}




		public string GetPhraseByID(string namespacename, string id, string originalphrase)
		{
			return GetPhraseByID(namespacename, id, originalphrase, CultureInfo.CurrentUICulture);
		}

		public string GetPhraseByID(string namespacename, string id, string originalphrase, CultureInfo locale)
		{
			return GetPhraseByID(namespacename, id, originalphrase, locale.Name);
		}

		public string GetPhraseByID(string namespacename, string id, string originalphrase, string locale)
		{
			namespacename = namespacename.ToLower();

			string ret = originalphrase;

			if (namespaces.ContainsKey(namespacename))
			{
				LibraryNamespace libns = namespaces[namespacename];
				ret = libns.GetPhraseByID(id, originalphrase, locale);
			}

			return ret;
		}




		public bool RegisterPhrase(string namespacename, string originalphrase, string translation, CultureInfo locale)
		{
			return RegisterPhrase(namespacename, originalphrase, translation, locale.Name);
		}

		public bool RegisterPhrase(string namespacename, string originalphrase, string translation, string locale)
		{
			namespacename = namespacename.ToLower();
			if (namespaces.ContainsKey(namespacename))
			{
				namespaces[namespacename].RegisterPhrase(originalphrase, translation, locale);
			}
			else
			{
				LibraryNamespace newns = new LibraryNamespace(this);
				newns.RegisterPhrase(originalphrase, translation, locale);
				namespaces.Add(namespacename, newns);
			}

			return true;
		}


		public bool RegisterPhraseID(string namespacename, string id, string originalphrase, string translation, CultureInfo locale)
		{
			return RegisterPhraseID(namespacename, id, originalphrase, translation, locale.Name);
		}

		private bool RegisterPhraseID(string namespacename, string id, string originalphrase, string translation, string locale)
		{
			namespacename = namespacename.ToLower();
			if (namespaces.ContainsKey(namespacename))
			{
				namespaces[namespacename].RegisterPhraseID(id, originalphrase, translation, locale);
			}
			else
			{
				LibraryNamespace newns = new LibraryNamespace(this);
				newns.RegisterPhraseID(id, originalphrase, translation, locale);
				namespaces.Add(namespacename, newns);
			}

			return true;
		}
		 

		/*

		public int PhraseCount
		{
			get
			{
				int ret = 0;
				Dictionary<string, LibraryNamespace>.Enumerator e1 = namespaces.GetEnumerator();
				while (e1.MoveNext())
					ret += e1.Current.Value.PhraseCount;
				return ret;
			}
		}

		public int LocaleCount
		{
			get
			{
				return locales.Count;
			}
		}

		public int NamespaceCount
		{
			get
			{
				return namespaces.Count;
			}
		}

		 */
	}
}
